<template>
  <div>
    <el-row type="flex" class="firstRow">
      <el-col :span="3">
        <el-button type="primary" round size="small" @click="go"
          >配置列表</el-button
        >
      </el-col>
      <el-col :span="3">
        <el-button type="success" round size="small" @click="open"
          >添加配置</el-button
        >
      </el-col>
      <el-col :span="18"> </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table
          :data="tableData"
          style="width: 100%"
          height="500"
          v-loading="loading"
        >
          <el-table-column prop="id" label="ID"> </el-table-column>
          <el-table-column prop="info" label="配置名称"> </el-table-column>
          <el-table-column prop="menu_name" label="字段变量"> </el-table-column>
          <el-table-column prop="type" label="字段类型"> </el-table-column>
          <el-table-column prop="value" label="值">
            <template slot-scope="scope">
              <div v-if="Array.isArray(scope.row.value)">
                <img :src="scope.row.value[0].filepath" />
                {{ scope.row.value[0].filename }}
              </div>
              <div v-else>
                {{ scope.row.value }}
              </div>
            </template>
          </el-table-column>

          <el-table-column prop="status" label="状态">
            <template slot-scope="scope">
              <Sw
                :isShow="scope.row.status"
                :editId="scope.row.id"
                @switchChange="switchChange"
              ></Sw>
            </template>
          </el-table-column>

          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button
                @click="handleClick(scope.row)"
                type="text"
                size="small"
                >查看</el-button
              >
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-delete-solid"
                icon-color="red"
                title="确定删除吗？"
                @confirm="del({ ...scope.row })"
                style="margin-left:5px"
              >
                <el-button slot="reference" type="text" size="small"
                  >删除</el-button
                >
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <DataModel
      :dialogFormVisible="dialogFormVisible"
      :form="form"
      :title="title"
      :type="type"
      :ruleList="ruleList"
      @changeSelect="changeSelect"
      @finish="finish"
    />
  </div>
</template>

<script>
import {
  tab,
  switchfun,
  dellist,
  getAddRule,
  querenAdd,
  getEditRule,
  querenEdit,
} from "../../api/Maintain";
import Sw from "../../components/Sw.vue";
import DataModel from "../../components/MaintainDialog/DataModel";
export default {
  components: { Sw, DataModel },
  data() {
    return {
      tableData: [],
      loading: false,
      dialogFormVisible: false,
      title: "",
      form: {
        config_tab_id: "",
        input_type: "",
        info: "",
        desc: "",
        value: "",
        width: 100,
        required: "",
        sort: "",
        status: 1,
        upload_type: 1,
        parameter: "",
        high: "",
      },
      tab_id: 0,
      type: 0,
      ruleList: [],
    };
  },

  methods: {
    open() {
      this.openModel();
    },
    //模态框里边的select变化
    changeSelect(e) {
      console.log(e);
      this.type = e;
      this.openModel();
    },
    //点击切换
    openModel() {
      const Lo = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.form = {
        config_tab_id: "",
        input_type: "",
        info: "",
        desc: "",
        value: "",
        width: 100,
        required: "",
        sort: 1,
        status: 1,
        upload_type: 1,
        parameter: "",
        high: 1,
        menu_name: "",
      };
      const { tab_id, type } = this;
      getAddRule({ tab_id, type }).then((res) => {
        this.ruleList = res.data.data.rules;
        this.dialogFormVisible = res.data.data.status;
        this.title = res.data.data.title;
        Lo.close();
      });
    },
    //点击模态框确认
    finish(e) {
      e.type = this.type;
      if (e.id) {
        querenEdit(e).then((res) => {
          this.getData();
          this.dialogFormVisible = false;
        });
      } else {
        querenAdd(e).then((res) => {
          this.getData();
          this.dialogFormVisible = false;
        });
      }
    },
    //点击配置列表
    go() {
      this.$router.push("/maintain");
    },
    handleClick(row) {
      this.form = {
        config_tab_id: row.config_tab_id,
        input_type: row.input_type,
        info: row.info,
        desc: row.desc,
        value: row.value,
        width: row.value,
        required: row.required,
        sort: row.required,
        status: row.status,
        upload_type: row.upload_type,
        parameter: row.parameter,
        high: row.high,
        id: row.id,
        menu_name: row.menu_name,
      };
      const Lo = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });

      getEditRule(row.id).then((res) => {
        this.ruleList = res.data.data.rules;
        this.dialogFormVisible = res.data.data.status;
        this.title = res.data.data.title;
        Lo.close();
      });
    },
    //修改switch状态
    switchChange(e) {
      this.loading = true;
      switchfun(e).then((res) => {
        this.getData();
      });
    },
    //获取数据
    getData() {
      this.loading = true;
      let obj = {
        tab_id: this.$route.query.id,
        page: 1,
        limit: 20,
      };
      tab(obj).then((res) => {
        console.log(res.data);
        this.tableData = res.data.data.list;
        this.loading = false;
      });
    },
    //删除
    del(e) {
      this.loading = true;
      dellist(e.id).then((res) => {
        this.getData();
      });
    },
  },
  created() {
    if (this.$route.query.id) {
      this.loading = true;
      this.getData();
      this.tab_id = this.$route.query.id;
    }
  },
};
</script>
<style lang="scss" scoped>
.firstRow {
  margin: 10px 0;
}
.el-col-12 {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
